<template>
  <el-dialog
    width="600px"
    :title="$t('设置分组')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel">
    <el-form :model="formData" ref="queryForm" size="small" inline label-width="90px">
      <el-form-item :label="$t('当前sku')" prop="skuNo">
        <span>{{ formData.skuNo }}</span>
      </el-form-item>
    </el-form>
    <el-table
      @selection-change="(rows) => (tableCheck = rows)"
      :height="calTableHeight() - 200"
      :data="tableData"
      ref="multipleTables">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="groupName"
        :label="$t('分组名')"
        :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleSubmit" type="primary">
        {{ $t('确定') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getCloudGroup ,setSkuCloudGroup } from '@/api/distribution/sku/assemblyList'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      tableCheck: [],
      form: {},
      // 表单校验
      rules: {},
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },

  methods: {
    /**切换 */
    checkuType() {},
    /**设置哪一行选中 */
    selectable() {
      this.$nextTick(() => {
        this.tableData.forEach((row) => {
          if (row.isChoose) {
            this.$refs.multipleTables.toggleRowSelection(row, true)
          }
        })
      })
    },
    getList() {
      getCloudGroup({ skuId: this.formData.skuId }).then((res) => {
        this.tableData = res.data
        this.selectable(this.tableData)
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
    handleSubmit() {
      const groupIdList = this.tableCheck.map((i) => i.id)
      setSkuCloudGroup({
        skuId: this.formData.skuId,
        groupIdList,
      }).then((res) => {
        this.$message({
          type:'success',
          message:'操作成功'
        })
        this.$emit('getList')
        this.$emit('update:show', false)
      })
    },
  },
}
</script>
<style scoped lang="scss"></style>
